package pojo.other;

import Mysqlcon.Conn;
import uilt.Scan;

import java.sql.ResultSet;

public class questions extends Conn {
    Scan sc = new Scan();

    public void menu(){
        while (true) {
            System.out.println("******欢迎来到试题资源管理******");
            System.out.println("1.添加试题");
            System.out.println("2.试题资源列表");
            System.out.println("3.删除试题");
            System.out.println("4.退       出");
            System.out.print("请输入:");
            int i = sc.getkey();

            if (i == 1) {
                System.out.println("******欢迎来到添加试题******");
                System.out.print("请输入题干:");
                String title = sc.getString();
                System.out.print("请输入A选项内容:");
                String A = sc.getString();
                System.out.print("请输入B选项内容:");
                String B = sc.getString();
                System.out.print("请输入C选项内容:");
                String C = sc.getString();
                System.out.print("请输入D选项内容:");
                String D = sc.getString();
                System.out.print("请输入正确选项：");
                String bingo = sc.getString();
                System.out.print("请输入绑定知识点：");
                String knowledge = sc.getString();
                insertQuestions(title,A,B,C,D,bingo,knowledge);

            } else if (i == 2) {
                showQuestions();
            }
            else if(i == 3){
                System.out.println("******欢迎来到删除试题资源******");
                System.out.print("请输入相关知识点:");
                String knowledge = sc.getString();
                deleteQuestions(knowledge);
            }
            else {
                break;
            }
        }

    }

    //添加试题
    public void insertQuestions(String title,String A,String B,String C,String D,String bingo,String knowledge){

        try {
            String sql = "insert into questions values(?,?,?,?,?,?,?,NOW())";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,title);
            prepareStatement.setString(2,A);
            prepareStatement.setString(3,B);
            prepareStatement.setString(4,C);
            prepareStatement.setString(5,D);
            prepareStatement.setString(6,bingo);
            prepareStatement.setString(7,knowledge);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，试题添加成功！");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //试题资源列表
    public void showQuestions(){
        try{
            sta = connection.createStatement();

            String sql = "select * from questions";

            ResultSet rs = sta.executeQuery(sql);

            while(rs.next()){
                System.out.println("题干:"+rs.getString("questiontitle")+"\t选项A："+rs.getString("chose_A")+"\t选项B："+rs.getString("chose_B")+"\t选项C："+rs.getString("chose_C")+"\t选项D："+rs.getString("chose_D")+"\t正确选项："+rs.getString("bingo")+"\t绑定知识点："+rs.getString("knowlegde")+"\t;创建时间:"+rs.getString("inserttime"));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    //删除试题列表
    public void deleteQuestions(String knowledge){
        try {
            String sql = "delete from questions where knowlegde = ?";
            prepareStatement = connection.prepareStatement(sql);
            prepareStatement.setString(1,knowledge);
            int num = prepareStatement.executeUpdate();
            if (num > 0) {
                System.out.println("恭喜，删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
